$(function () {
	yanzheng($("#myPhone"),$("#code"));
});

//验证手机格式


//验证发送时间
var nums = 60;
function yanzheng(pNumber,code){ //验证码验证
	var conOff = false;
	var clock = '';
	pNumber.bind('input propertychange',function(){
		if( $(this).val().length == 11 ){
			if (isPhone($(this).val())) {
				if(nums ==60 || nums ==0){
					code.css({
						background:"#0DB664",
						color:"#fff"
					});
					conOff = true;
				}
			}else{
				easyAlert("请输入有效的手机号.");
				code.css({
					background:"#fff",
					color:"#eaeaea"
				});
				conOff = false;
			}
		}else{
			code.css({
				background:"#fff",
				color:"#eaeaea"
			});
			conOff = false;
		};
	})
	code.bind('click',function(){
		if( conOff == true){
			checkPhoneIsExist(code);

		}
	});
	function checkPhoneIsExist() {
		var supply = $("#isSupply").val();
		$.ajax({
			type : 'post',
			url : server_url + 'rest/frontUser/checkPhone.do?phone='
					+ $('#myPhone').val()+"&supply="+supply+"&ran="+Math.random(),
			dataType : 'json',
			async : false,
			error:function(){
				conOff = true;
				code.css({
					background:"#0DB664",
					color:"#fff"
				});
			},
			success : function(data) {
				if (data.retCode == "0000") {
					alert('该手机号码已经存在,请重新输入！');
					flag = true;
				}else{
					sendCode(code);
					flag = false;
				}
			}
		});
		return flag;
	}
	
	function sendCode(code){//倒计时
		conOff = false; //将按钮置为不可点击
		code.css({
			background:"#fff",
			color:"#eaeaea"
		});
		code.html(nums+'秒后重新获取');
		clock = setInterval(doLoop, 1000); //一秒执行一次
		$.ajax({
			type:'post',
			url:server_url+'rest/verify/sendCode.do?phone='+pNumber.val()+"&ran="+Math.random(),
			dataType:'json',
			async : false,
			error:function(){
				alert('网络异常'); 
				conOff = true;
				code.css({
					background:"#0DB664",
					color:"#fff"
				});
			},
			success:function(data){
				if(localStorage){
					localStorage.setItem("phoneNoUUID",data.data.codeUuid);
				}
				easyAlert("短信已发送,请查收!");
			}
		});
		
	};
	function doLoop(){//倒计时定时器
		nums--;
		if(nums > 0){
			code.html(nums+'秒后重新获取');
		}
		else{
		  	clearInterval(clock); //清除js定时器
		  	code.html('重新发送验证码');
		  	code.css({
				background:"#0DB664",
				color:"#fff"
			});
		  	nums = 60; //重置时间
		  	conOff = true;
		};
	};
}